package cn.crm.Service.Impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.crm.Service.IRoleService;
import cn.crm.common.ServerResponse;
import cn.crm.entity.Permission;
import cn.crm.entity.Role;
import cn.crm.entity.RolePermission;
import cn.crm.entity.RolePermissionKey;
import cn.crm.mapper.PermissionMapper;
import cn.crm.mapper.RoleMapper;
import cn.crm.mapper.RolePermissionMapper;
import cn.crm.vo.LayuiMVO;
@Service
public class RoleServicceImpl implements IRoleService{

	@Autowired
	private RoleMapper rolemapper;
	@Autowired
	private RolePermissionMapper rolepermissionmapper;
	@Autowired
	private PermissionMapper permissionmapper;
	
	@Override
	public ServerResponse pageList(Integer page, Integer limit, Role role) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page, limit);
		List list = rolemapper.pageList(role);
		PageInfo pageInfo = new PageInfo<>(list);
		int count = (int) pageInfo.getTotal();
		return ServerResponse.createSuccess("查询成功", count, list);
	}

	@Override
	public ServerResponse selectAllRoles() {
		// TODO Auto-generated method stub
		//这里我们之所以生成一个new Role()是因为我们这个方法不传值
		List<Role> list = rolemapper.pageList(new Role());
		List<LayuiMVO> layuiMVOs = new ArrayList<>();
		for (Role role : list) {
			LayuiMVO layuiMVO = new LayuiMVO();
			layuiMVO.setId(role.getId());
			layuiMVO.setName(role.getSn());
			layuiMVO.setStatus(1);
			layuiMVOs.add(layuiMVO);
		}
		return ServerResponse.createSuccess("查找成功", layuiMVOs);
	}

	@Override
	public ServerResponse insert(Role role, String permissions) {
		// TODO Auto-generated method stub
		int count = rolemapper.insert(role);
		int count2 = 0;
		String[] permissionids = permissions.split(",");
		for (String permissionId : permissionids) {
			RolePermission rolePermission = new RolePermission(role.getId(),Integer.parseInt(permissionId));
			count2 = rolepermissionmapper.insert(rolePermission);
		}
		if (count == 1 && count2 == 1) {
			return ServerResponse.createSuccess("添加成功");
		}else {
			return ServerResponse.createError("添加失败");
		}
	}

	@Override
	public ServerResponse selectRoleandPermissions(Integer roleId) {
		// TODO Auto-generated method stub
		Map map = new HashMap<>();
		Role role = rolemapper.selectByPrimaryKey(roleId);
		map.put("role", role);
		List<Permission> pageList = permissionmapper.pageList(new Permission());
		List<LayuiMVO> layuiMVOs = new ArrayList<>();
		for (Permission permission : pageList) {
			LayuiMVO layuiMVO = new LayuiMVO();
			layuiMVO.setId(permission.getId());
			layuiMVO.setName(permission.getName());
			layuiMVO.setStatus(1);
			layuiMVOs.add(layuiMVO);
		}
		map.put("layuiMVOs", layuiMVOs);
		List<Integer> lists = rolepermissionmapper.selectByRoleId(roleId);
		map.put("lists", lists.toArray());
		return ServerResponse.createSuccess("查找成功", map);
	}

	@Override
	public ServerResponse update(Role role, String permissions) {
		// TODO Auto-generated method stub
		rolemapper.updateByPrimaryKeySelective(role);
		int roleId = role.getId();
		rolepermissionmapper.deleteById(roleId);
		String[] permissionIds = permissions.split(",");
		for (String permissionId : permissionIds) {
			RolePermission rolePermission = new RolePermission(roleId, Integer.parseInt(permissionId));
			rolepermissionmapper.insert(rolePermission);
		}
		return ServerResponse.createSuccess("修改成功");
	}

}
